
$(document).ready(function() {

    $("#formUsername").submit(function(event) {
        // stop form from submitting normally
        event.preventDefault();

        // check if the user entered a valid username
//        var x=document.forms["formUsername"]["username"].value;
//        if (x==null || x=="")
//        {
//            alert("Palun sisesta kasutajanimi.");
//            return false;
//        } else if (!x.match(/^[a-z0-9]+$/i)) {
//            alert("Kasutajanimi võib sisaldada ainult tähti ja numbreid (täpitähed pole lubatud).");
//            return false;
//        }

//        FB.api('/me', function(response) {
//        	 // alert('Your name is ' + response.name);

//        	  username= response.name;
//        	  if (response.name === undefined){
//        		  console.log("sa pole sees");
//        		  return false;
//        	  }
////        	  else if (!response.name.match(/^[a-z0-9]+$/i)) {
////                alert("Kasutajanimi võib sisaldada ainult tähti ja numbreid (täpitähed pole lubatud).");
////        	  }
//
//        	});

        // get information from form
        var $form = $(this),
          //  username = $form.find("input[name='username']").val(),
            url = $form.attr("action");

        // send information, save ID in localstorage, then go to lobby
        $.post(url, {username: username},
            function(data) {
                if (!data) {
                    alert("Sellise kasutajanimega mängija on juba sisse logitud.");
                    return;
                }
                localStorage.setItem("id", data);
                localStorage.setItem("username", username);
                window.location.replace("lobby.jsp?id=" + data);
            });
    });




    // testing
//    localStorage.setItem("LastGameShootInfo", "[[[1,1,1,0],[1,3,1,0],[3,2,3,0],[4,2,1,0],[6,2,3,0],[7,3,3,1],[10,7,2,0],[9,7,2,0],[8,7,1,0],[8,10,2,0],[9,10,2,0],[6,9,2,0],[5,9,2,0],[4,9,2,0],[3,9,2,0],[3,7,2,0],[4,7,2,0],[2,7,2,0],[2,5,2,0],[1,5,2,0],[5,4,1,0],[7,2,1,0]],[[2,10,3,1],[3,10,2,0],[4,10,2,0],[5,10,2,0],[6,10,2,0],[1,8,2,0],[2,8,2,0],[7,8,2,0],[8,8,2,0],[2,6,2,0],[3,6,2,0],[4,6,2,0],[6,6,2,0],[7,6,2,0],[2,4,2,0],[3,4,2,0],[1,2,1,0],[3,2,1,0],[5,2,3,0],[9,3,3,1],[6,2,1,0],[8,2,3,0],[9,3,3,1],[9,2,1,0],[8,4,3,0],[7,2,3,1],[3,8,2,0],[4,8,3,0],[4,6,2,1],[7,6,3,1]],[[1,10,2,0],[1,9,2,0],[1,8,2,0],[1,7,2,0],[1,6,3,0],[3,10,2,1],[2,10,2,1],[4,10,2,1],[5,10,2,1],[6,10,3,1],[1,3,1,0],[3,3,1,0],[5,3,1,0],[7,3,1,0],[1,5,2,0],[2,5,2,0],[4,5,2,0],[5,5,2,0],[7,5,2,0],[8,5,2,0],[6,8,2,0],[7,8,2,0],[8,8,3,0],[6,10,3,1],[6,10,2,0],[7,10,2,0],[8,10,3,0],[9,5,3,1],[5,8,2,0],[5,10,2,0],[4,10,3,0],[7,4,3,1]]]");
//    var games = [];
//    var game1 = {"date": new Date().toLocaleString(), "username": "Peeter", "opponent": "Mari"};
//    var game2 = {"date": new Date().toLocaleString(), "username": "Kaja", "opponent": "Jüri"};
//    var game3 = {"date": new Date().toLocaleString(), "username": "Sofi", "opponent": "Toby"};
//    games.push(game1); games.push(game2); games.push(game3);
//    localStorage.setItem("PlayersInfo", JSON.stringify(games));

});
